﻿using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AdvanceGridSearchCrm2013
{
    public partial class AdVancedCrmSearch : System.Web.UI.UserControl
    {
        private string m_Fetch_xml;
        private readonly IOrganizationService m_service;
        private Dictionary<string, string> m_columnsHeaderAndMaps;

        public Dictionary<string, string> ColumnsHeaderAndMaps
        {
            get { return m_columnsHeaderAndMaps; }
            set { m_columnsHeaderAndMaps = value; }
        }
        public string Fetch_xml
        {
            get { return m_Fetch_xml; }
            set { m_Fetch_xml = value; }
        }
        public AdVancedCrmSearch()
        {
            m_service = OrgnizationServiceHandler.GetService();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            QueryUtility query = new QueryUtility();
            DataTable datasource = query.ExecuteFetchXml(m_Fetch_xml, m_service);

            crmrepeater.HeaderTemplate = new MyTemplate(ListItemType.Header, ColumnsHeaderAndMaps);
            crmrepeater.ItemTemplate = new MyTemplate(ListItemType.Item, ColumnsHeaderAndMaps);

            crmrepeater.FooterTemplate = new MyTemplate(ListItemType.Footer, ColumnsHeaderAndMaps);

            crmrepeater.DataSource = datasource;
            crmrepeater.DataBind();


        }
    }
    public class MyTemplate : System.Web.UI.ITemplate
    {
        System.Web.UI.WebControls.ListItemType templateType;
        public MyTemplate(System.Web.UI.WebControls.ListItemType type, Dictionary<string, string> ColumnsHeaderAndMaps)
        {
            templateType = type;
            m_ColumnsHeaderAndMaps = ColumnsHeaderAndMaps;
        }
        private static Dictionary<string, string> m_ColumnsHeaderAndMaps;

        public void InstantiateIn(System.Web.UI.Control container)
        {
            
            PlaceHolder ph = new PlaceHolder();
            string headerhtml = String.Empty;
            foreach (var header in m_ColumnsHeaderAndMaps)
            {
                headerhtml += string.Format("<th>{0}</th>", header.Value);

            }
            switch (templateType)
            {
                case ListItemType.Header:
                    ph.Controls.Add(new LiteralControl(@"<table id='dataTable'>"
                                                       + "<thead>"
                                                        + headerhtml
                                                          + "</thead>"));


                    break;
                case ListItemType.Item:
                    ph.Controls.Add(new LiteralControl("<tr>"));
                    foreach (var header in m_ColumnsHeaderAndMaps)
                    {
                        ph.Controls.Add(new LiteralControl(" <td>"));
                      
                        System.Web.UI.WebControls.Label item = new System.Web.UI.WebControls.Label();
                        item.ID = header.Key;
                        ph.Controls.Add(item);
                        ph.Controls.Add(new LiteralControl("</td>"));
                    }
                      ph.Controls.Add(new LiteralControl("</tr>"));
                    ph.DataBinding += new EventHandler(Item_DataBinding);

                 
                    break;

                case ListItemType.Footer:
                    ph.Controls.Add(new LiteralControl("</table>"));
                    break;
            }
            container.Controls.Add(ph);
        }
        static void Item_DataBinding(object sender, System.EventArgs e)
        {
            PlaceHolder ph = (PlaceHolder)sender;
            RepeaterItem ri = (RepeaterItem)ph.NamingContainer;

            foreach (var item in m_ColumnsHeaderAndMaps)
            {
                 DataRowView drvw= (DataRowView)ri.DataItem;

                  if (drvw.Row.Table.Columns.Contains(item.Key) && DataBinder.Eval(ri.DataItem, item.Key) != System.DBNull.Value)
                {
                
                  string  itemValue = (String)DataBinder.Eval(ri.DataItem, item.Key);
                  ((System.Web.UI.WebControls.Label)ph.FindControl(item.Key)).Text = itemValue.ToString();
                }
            }

           
        }



    }


}